<?php
/**
 * The generator for all permutation
 * 
 * @author lhe
 */
class NumberGenerator
{
    /**
     * NumberDB
     * @var NumberDB
     */
    private $_db = null;
    /**
     * The id of the game type
     * 
     * @var int
     */
    private $_gameTypeId;
    /**
     * The insert header statement
     * 
     * @var string
     */
    private $_sqlheader = '';
    /**
     * 
     * constructor
     */
    public function __construct()
    {
        $this->_db = new NumberDB();
    }
    /**
     * getting each permutations
     * 
     * @param array $v     An array that contains all the numbers
     * @param int   $start  The start number for each element
     * @param int   $n      The max number for each element
     * @param int   $k      The combination index flag
     * @param int   $maxk   The total number of number for each combination
     * @param bool  $genSql Whether we are generating for a sql.
     */
    private function _combinations (array $v, $start, $n, $k, $maxk, $genSql = false)
    {
        $combinations = array();
        /* k here counts through positions in the maxk-element v.
         * if k > maxk, then the v is complete and we can use it.
        */
        if ($k > $maxk) 
        {
            /* insert code here to use combinations as you please */
            for ($i=1; $i<=$maxk; $i++) 
            {
                $combinations[] = $v[$i];
            }
            $this->_formatPermutations($combinations, $genSql);
            return;
        }
    
        /* for this k'th element of the v, try all start..n
         * elements in that position
        */
        for ($i=$start; $i<=$n; $i++) 
        {
            $v[$k] = $i;
            /* recursively generate combinations of integers
             * from i+1..n
            */
            $this->_combinations ($v, $i+1, $n, $k+1, $maxk, $genSql);
        }
    }
    /**
     * Formating the permutation
     *  
     * @param array $combinations The permutations that we are formating
     * @param bool  $genSql       Whether we are generating for a sql.
     */
    private function _formatPermutations($combinations, $genSql = false)
    {
        sort($combinations);
        $flatted = implode(", ", $combinations);
        if ($genSql === true)
            $this->_db->newPermutation($combinations, $this->_gameTypeId); 
        else
            var_dump($flatted);
    }
    /**
     * generates the sql to insert into Number table
     * 
     * @param int  $gameTypeId The Id of the game type
     * @param bool $insertDb   Whether we will do the insert to the database, or just print outs
     */
    public function gen($gameTypeId, $insertDb = false)
    {
        $gameType = $this->_db->getGameType($gameTypeId);
        if(!isset($gameType['id']) || !is_numeric(($gameType['id'])))
            throw new Exception('Invalid game type id(= ' . $gameTypeId . ') provided!');
        $howManyPerRow = trim($gameType['noPerRow']);
        $maxNumber = trim($gameType['maxNo']);
        $this->_combinations(range(1, $maxNumber), 1, $maxNumber, 1, $howManyPerRow, true);
    }
}
?>